package com.shen.JUC.fockjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class Test1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//test1();783
//test2(); 442
        test3(); //65
    }
    //普通计算方法
    public static void test1(){
        Long sum=0L;
        long start = System.currentTimeMillis();
        for (Long i = 1L; i <1_0000_0000 ; i++) {
            sum +=i;
        }
        long end = System.currentTimeMillis();
        System.out.println(sum+"时间 "+(end-start));
    }
    //forkJoin计算
    public static void test2() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();

        ForkJoinPool pool = new ForkJoinPool();
        Add add = new Add(1L, 1_0000_0000L);
        Long sum = pool.submit(add).get();

        long end = System.currentTimeMillis();
        System.out.println(sum+"时间 "+(end-start));
    }
    //Stream流
    public static void test3(){
        long start = System.currentTimeMillis();

        long sum = LongStream.range(1L, 1_0000_0000).sum();

        long end = System.currentTimeMillis();
        System.out.println(sum+"时间 "+(end-start));
    }
}
